<template>
  <strong>Vue后台</strong>
  <div class="hander-title">
    <el-avatar
      :src="userInfo.avatar"
    ></el-avatar>
    
    <el-dropdown>
      <span class="el-dropdown-link">
        {{userInfo.username}}
        <el-icon class="el-icon--right">
          <arrow-down />
        </el-icon>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item @click="toLink('userCenter')">个人中心</el-dropdown-item>
          <el-dropdown-item @click="logout">退出</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
    <el-link type="info">关注我</el-link>
    <el-link type="info">入门</el-link>
  </div>
</template>

<script lang="ts">
import { defineComponent, nextTick, onMounted, reactive } from "vue";
import { useRouter } from "vue-router";
import {useStore} from 'vuex';
import {LogOut, UserInfo} from '../axios/index';
import emitter from "../plugins/evenHub";

export default defineComponent({
  name: "HanderMenu",
  setup() {
    let userInfo = reactive({
      id: '',
      username: '',
      avatar: ''
    });

    const store = useStore()


    const router = useRouter()

    const getUserInfo = async ()=>{
      const {data} = await UserInfo();
      userInfo.id = data.data.id
      userInfo.username = data.data.username
      userInfo.avatar = data.data.avatar
      // console.log(data.data);
      console.log("userIfo:"+ userInfo.avatar);
    }

    onMounted(()=>{
      getUserInfo()
    } );

    const toLink = (routerName: any)=>{
          emitter.emit("addTable", {
          title: "个人中心",
          name: "6",
        });
      router.push({name: routerName})
    }

    const logout = async ()=>{
      const data = await LogOut()
      console.log("用户退出");
      localStorage.clear()
      sessionStorage.clear()
      store.commit("login/RESETSTATE");
      router.push({name: 'login'})
    }



    return{
      userInfo,
      toLink,
      logout
    }


  },
});
</script>


<style lang="less" scoped>
.hander-title {
  width: 200px;
  float: right;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
</style>